package com.yupi.springbootinit.controller;
import java.beans.Beans;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.hutool.aop.ProxyUtil;
import cn.hutool.aop.aspects.TimeIntervalAspect;
import cn.hutool.core.bean.BeanUtil;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.Leisure;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.site.SiteAddRequest;
import com.yupi.springbootinit.model.dto.site.SiteUpdateRequest;
import com.yupi.springbootinit.model.entity.Leisuresite;
import com.yupi.springbootinit.model.entity.Site;
import com.yupi.springbootinit.model.vo.SiteVo;
import com.yupi.springbootinit.service.LeisuresiteService;
import com.yupi.springbootinit.service.SiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author xingyu Xue
 * @Date 2024/4/27 13:32
 */
@Api(value = "场地接口")
@RestController
@RequestMapping("/site")
public class SiteController {
    @Autowired
    private SiteService siteService;
    @Autowired
    private LeisuresiteService leisuresiteService;
    @ApiOperation(value ="添加")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/add")
    public BaseResponse<Site> addSite(@RequestBody SiteAddRequest siteAddRequest, HttpServletRequest httpServletRequest){
        //判断两个参数是否为空
        if (siteAddRequest == null || siteAddRequest.getBelongs() == null){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer siteName = siteAddRequest.getSiteName();
        Integer belongs = siteAddRequest.getBelongs();
        Integer siteArea = siteAddRequest.getSiteArea();
        String sitetMain = siteAddRequest.getSitetMain();
        Site addSiteResult = siteService.addSite(siteName, belongs, siteArea, sitetMain);



        //新增site成功
        return ResultUtils.success(addSiteResult);
    }

    @ApiOperation(value = "更新场地空闲信息")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody SiteUpdateRequest updateRequest,HttpServletRequest httpServletRequest) throws ParseException {
        if (updateRequest == null || updateRequest.getId() == null){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
//        Site site=new Site();
//        BeanUtils.copyProperties(updateRequest,site);
        boolean update = siteService.updateFreeTime(updateRequest);
        if (!update){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        return ResultUtils.success(true);

    }


    @ApiOperation(value = "获取所有球场信息")
    @PostMapping("/all")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<SiteVo>> getAllSite(HttpServletRequest httpServletRequest){
        List<Site> allSite = siteService.getAllSite(httpServletRequest);
        //将list<site>集合中的对象都替换为VO对象
        List<SiteVo> RsiteVo =new ArrayList<>();
        for (Site site : allSite) {
            SiteVo siteVo =new SiteVo();
            BeanUtil.copyProperties(site,siteVo);
            RsiteVo.add(siteVo);
        }

        return ResultUtils.success(RsiteVo);
    }




}
